// 加法
export function numberAdd(arg1, arg2) {
	if ((!arg1 || !arg2) && arg1 != 0 && arg2 != 0) {
		console.log('Error: 加法需要传入2个数字')
		return
	}
	let arg1Length = 0 // a的小数部分长度
	let arg2Length = 0 // b的小数部分长度
	try {
		arg1Length = arg1.toString().split('.')[1].length
	} catch (f) {}
	try {
		arg2Length = arg2.toString().split('.')[1].length
	} catch (f) {}

	let resNum = 10 ** Math.max(arg1Length, arg2Length) // 保证a、b为整数的最小10次幂
	return (arg1 * resNum + arg2 * resNum) / resNum
}

// 减法
export function numberSub(arg1, arg2) {
	if ((!arg1 || !arg2) && arg1 != 0 && arg2 != 0) {
		console.log('Error: 减法运算需要传入2个数字')
		return
	}
	let arg1Length = 0 // a的小数部分长度
	let arg2Length = 0 // b的小数部分长度
	try {
		arg1Length = arg1.toString().split('.')[1].length
	} catch (f) {}
	try {
		arg2Length = arg2.toString().split('.')[1].length
	} catch (f) {}

	let resNum = 10 ** Math.max(arg1Length, arg2Length) //保证a、b为整数的最小10次幂
	return (arg1 * resNum - arg2 * resNum) / resNum
}

// 乘法
export function numberMul(arg1, arg2) {
	if ((!arg1 || !arg2) && arg1 != 0 && arg2 != 0) {
		console.log('Error: 乘法运算需要传入2个数字')
		return
	}
	let arg1Length = 0 // a的小数部分长度
	let arg2Length = 0 // b的小数部分长度
	try {
		arg1Length = arg1.toString().split('.')[1].length
	} catch (f) {}
	try {
		arg2Length = arg2.toString().split('.')[1].length
	} catch (f) {}

	return (Number(arg1.toString().replace('.', '')) * Number(arg2.toString().replace('.', ''))) / (10 ** (arg1Length +
		arg2Length))
}

// 除法
export function numberDiv(arg1, arg2) {
	if ((!arg1 || !arg2) && arg1 != 0) {
		console.log('Error: 减法运算需要传入2个数字')
		return
	}
	let arg1Length = 0 // a的小数部分长度
	let arg2Length = 0 // b的小数部分长度
	try {
		arg1Length = arg1.toString().split('.')[1].length
	} catch (f) {}
	try {
		arg2Length = arg2.toString().split('.')[1].length
	} catch (f) {}

	const molecule = Number(arg1.toString().replace('.', '')) * (10 ** (arg1Length + arg2Length))
	const denominator = Number(arg2.toString().replace('.', '')) * (10 ** (arg1Length + arg2Length))
	return molecule / denominator / (10 ** (arg1Length - arg2Length))
}

//处理三级分类
export const organizeArray = (items) => {
	// 根据tier分类  
	const tier0 = items.filter(item => item.tier === 0);
	const tier1 = items.filter(item => item.tier === 1);
	const tier2 = items.filter(item => item.tier === 2);
	// 初始化一级数组  
	const result = tier0.map(item => ({
		...item,
		children: []
	}));
	//父级id跟子级parentId进行关联
	tier1.forEach(item => {
		const parent = result.find(p => p.id === item.parentId);
		if (parent) {
			parent.children.push({
				...item,
				subchildren: []
			});
		}
	});
	tier2.forEach(item => {
		const parent = result.find(p => p.children.some(c => c.id === item.parentId));
		if (parent) {
			const child = parent.children.find(c => c.id === item.parentId);
			if (child) {
				child.subchildren.push(item);
			}
		}
	});
	return result;
}

export const handlePhone = (phone) => {
	var mobileRegex = /^1[3-9]\d{9}$/;
	if (mobileRegex.test(phone)) {  
	    return true
	} else {  
	    return false 
	}
}

export function forrmatterEnmu(list, value) {
	if(value == "" && value != 0) return '';
	let index = list.findIndex(ele => ele.value == value);
	return index > -1 ? list[index].label : value;
}

// 时间戳转换成-隔开
export function timestampToDate(timestamp, type = 'ys') {
	// 创建一个新的Date对象，直接使用时间戳
	var date;
	if(!timestamp || timestamp == '') {
		date = new Date();
	} else if(typeof timestamp == 'number') {
		date = new Date(timestamp);
	} else {
		date = new Date(timestamp.replace(/-/g,'/'));
	}
 
  // 获取年月日时分秒，并格式化为两位数字
  var year = date.getFullYear();
  var month = ('0' + (date.getMonth() + 1)).slice(-2); // 月份是从0开始的
  var day = ('0' + date.getDate()).slice(-2);
  var hours = ('0' + date.getHours()).slice(-2);
  var minutes = ('0' + date.getMinutes()).slice(-2);
  var seconds = ('0' + date.getSeconds()).slice(-2);
  if(type == 'ymd') {
	  return `${year}-${month}-${day}`
  } else if(type == 'ymdhm') {
	  return `${year}-${month}-${day} ${hours}:${minutes}`;
  }
 
  // 拼接字符串返回
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

import pageConfig from '@/config'
const baseUrl = pageConfig.baseUrl
import { getToken } from '@/utils/auth'
export function uploadFilePromise(url) {
	return new Promise((resolve, reject) => {
		let a = uni.uploadFile({
			url: baseUrl + '/common/upload', 
			filePath: url,
			name: 'file',
			header: {
				'Authorization': 'Bearer ' + getToken()
			},
			formData: {},
			success: (res) => {
				console.log(res)
				setTimeout(() => {
					resolve(JSON.parse(res.data))
				}, 1000)
			}
		});
	})
}

export function formatterImage(data) {
	var jsonDa = JSON.stringify(data).replace(/<img/gi, "<img class='richImg' style='width:100%!important;height:auto!important;max-height:100%;'");
	var newResData = JSON.parse(jsonDa);
	return newResData;
}
